// 引入算价组件
document.write("<script type='text/javascript' src='/statics/js/modules/component/calculatePriceComponent.js'></script>");
var erpOrderEditVm = new Vue({
	el:'#erpOrderEditapp',
	data: {
        orderNo:'',
        showEdit:false,
        title: null,
        erpOrder: {},
        e:{
            payTypeEnum:[],
        },
        q: {
            //客户名称查询
            name: '',
            //产品搜索
            productName: '',
            //商务印刷中的二级选项卡id
            printZiyunCategoryId: null,
            //快印印刷中的二级选项卡id
            fastZiyunCategoryId: null,
        },
        //门店业务员列表
        salesManList: [],
        /* 省市区data */
        area: [],
        //订单状态:
        productionStatusEnum: [{value: '7', label: '新开单'}, {value: '3', label: '生产中'}, {
            value: '4',
            label: '生产完成'
        }, {value: '6', label: '已完成'}],
        //订单类型
        orderSourceEnum: [],
        //商品明细列表
        orderItemDetailData: [],
        //单个商品的详情
        singleOrderItemDetail: {},
        //订单备注对象
        orderRemarks:{orderNo:'',orRemarks:''},
        //取消订单（添加一条记录到erp_order_log）
        erpOrderLogs:{orderNo:'',logText:''},
        options: [],
        //客户选择弹出框
        userDialogVisible: false,
        //添加产品弹出框
        productDialogVisible: false,
        //阶梯价格弹出框
        stepPirceDialogVisible: false,
        //单个商品详情弹出框
        orderItemDetailDialogVisible: false,
        //取消订单
        cancelOrderVisible:false,
        //添加订单是否显示可挂账
        credit:false,
        //收银对象
        recMoney:{},
        //客户列表
        userInfoData: [],
        //接单员列表:
        acceptOrderUserData: [],
        //阶梯价格数据:
        stepPriceData: [],
        //添加商品下方table数据
        goodsTableData: [],
        //订单详情table数据:
        detailData: [],
        //单个商品详情:
        singleItemData:[],
        formLabelWidth: '120px',
        sendType: [{value: '0', label: '快递'}, {value: '1', label: '自提'}],
        //支付方式
        payTypeData: ["支付宝", "微信", "现金"],
        currentUser: '',
        goodsData: [],
        //所有产品分类数据
        categoryData: {fast: {}, print: {}},
        defaultProps: {children: 'children', label: 'label'},
        //用于提交订单
        erpOrderItemDto: {},
        //商务印刷二级选项卡数据(store_goods_category里面的二级分类)
        shangWuCategoryData: [],
        //快印二级选项卡数据
        fastCategoryData: [],
        //报价部件集合
        quotePartsList: [],
        //类型
        categoryType: null,
        //存放当前支付方式
        erpOrderPayNow:'',
        quoteProductInfo: {
        },
        courrentUser:'', //当前用户
        sellerInfoEntity:{}, //商户信息

        printGoods:{},

        //快印报价价格框
        fastFactoryQuoteInfoList:[],
        //商务销售价格弹出框
        quotePriceDialogVisible:false,
        //快印销售价格弹出框
        fastQuotePriceDialogVisible:false,
        //存储选中的快印印刷索引
        fastIndex:0,
        //存放商务印刷销售金额
        printMoney:0,
        //存放快印销售金额：
        fastMoney:0,
        //存储选中的快印印刷产品
        fastGoods:{},

        //商务印刷相关信息
        quotePartShow:{},           // 部件是否展示
        quotePartsList:[],
        caculateQuoteData:{
            categoryType:'',         //分类
            attributesMap:{},       // 工艺属性
            selfPaperParts:{},      // 自来纸
            definitionValueMap:{},  // 自定义
        },
        material:{},                // 材料
        defineWheter:{},            // 开启定义信息
        defineArray:{},             // 自定义值数组
        craftWheter:{},             // 非必选工艺
        addCraft:{},                // 是否添加工艺
        quotePriceData:[],          //接收商务印刷返回的报价
        quotePriceVisible:false,    //是否展示算价相关信息
        quoteFastPriceVisible:false,
        quotePriceDialogVisible:false,
        //价格框
        factoryQuoteInfoList:[],
        printMoney:0,
        printIndex:2
    },
	methods: {
        loadData:function(data){
            erpOrderEditVm.orderNo=data.orderNo;
            erpOrderEditVm.title=data.title;
            erpOrderEditVm.getInfo( erpOrderEditVm.orderNo);
        },
		getInfo: function(orderNo){
		    //查询订单相关信息进行回显
            var self=this;
			$.get(baseURL + "order/erporder/updateInfo/"+orderNo, function(r){
			    if(r.code==0){
                    erpOrderEditVm.erpOrder = r.erpOrder;
                    erpOrderEditVm.detailData=r.orderItemDetailData;
                    erpOrderEditVm.dealAddon();
                    erpOrderEditVm.categoryData=r.categoryData;
                    erpOrderEditVm.categoryData.print=r.shangWuCategoryData
                    erpOrderEditVm.categoryData.fast=r.fastCategoryData;
                    erpOrderEditVm.showEdit=true;
                    setTimeout(function(){
                        erpOrderEditVm.$refs.calculateEditPrice.getMemberId(erpOrderEditVm.erpOrder.memberId);
                    })
                }else{
			        alert(r.msg);
                }

            });
		},
        dealAddon:function(){
            //处理数据
            if (erpOrderEditVm.detailData.length > 0) {
                for (var i = 0; i < erpOrderEditVm.detailData.length; i++) {
                    var item = erpOrderEditVm.detailData[i];
                    // cutModel 转换
                    if (!$.isEmptyObject(item.cutModel)) {
                        item.cutModelObj = JSON.parse(item.cutModel);
                    }
                    //处理工艺


                }
            }
        },
		//查看阶梯价
        seeStepPrice(row) {
		    erpOrderEditVm.stepPriceData=JSON.parse(row.stepPrice);
            erpOrderEditVm.stepPirceDialogVisible=true;
        },
        // 省市区
        handleItemChange: function (val) {
            var values = val.join(',').split(',');
            var lableVal =erpOrderEditVm.getCascaderObj(val, erpOrderEditVm.area);
            var area = lableVal.join(',').replace(new RegExp(',', 'g'), ' ');
           erpOrderEditVm.erpOrder.shipArea = area;
        },
        //弹出添加框
        addProductDialog:function () {
            erpOrderEditVm.productDialogVisible=true;
            erpOrderEditVm.goodsTableData=[];
            erpOrderEditVm.goodsData=[];
        },
        //分类被点击触发事件,查找当前分类下的材料
        handleNodeClick(data) {
            var  categoryId=data.categoryId;
            var memberId=erpOrderEditVm.erpOrder.memberId;
            if(isBlank(memberId)){
                memberId=99999999;//如果没选择客户，给个默认值
            }
            $.get(baseURL + "order/erporder/productInfo/"+categoryId+"/"+memberId, function(r){
                erpOrderEditVm.goodsData = r.goodsData;
            });
        },
        //产品搜索
        productSearch:function(){
            var memberId=erpOrderEditVm.erpOrder.memberId;
            if(isBlank(memberId)){
                memberId=99999999;//如果没选择客户，给个默认值
            }
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/productSearch",
                contentType: "application/x-www-form-urlencoded",
                dataType:"json",
                data:{ "productName":erpOrderEditVm.q.productName,"memberId":memberId},
                success: function(r){
                    if(r.code == 0){
                        erpOrderEditVm.goodsData=r.goodsData
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        zyAddGoods: function (data,goodsType) {
           if(goodsType=="standard"){
               erpOrderEditVm.goodsTableData=data;
               erpOrderEditVm.addToMainTable();
           }else if(goodsType=="fast"||goodsType=="print"){
               erpOrderEditVm.addFastGoods(data);
           }else{
               alert("获取商品类型错误，请联系管理员!");
           }
        },

        formatPricingManner:function(row, column, cellValue){
            if (cellValue == 0) {
                return '按面积计价'
            } else if (cellValue == 1) {
                return '按周长计价'
            }else  {
                return '按长度计价'
            }
        },
        //添加标品，直接向后台发起请求
        addToMainTable:function () {
		    //将添加标品的数据进行重新封装
            var goodData=[];
            for (let i = 0; i <erpOrderEditVm.goodsTableData.length ; i++) {
                var row={productId:'',name:'',addon:'',nums:'',price:'',amount:'',width:'',length:'',pricingManner:''};
                row.productId=erpOrderEditVm.goodsTableData[i].productId;
                row.name=erpOrderEditVm.goodsTableData[i].productName;
                row.addon=erpOrderEditVm.goodsTableData[i].craft;
                row.nums=erpOrderEditVm.goodsTableData[i].quantity;
                row.amount=erpOrderEditVm.goodsTableData[i].total;
                row.price=erpOrderEditVm.goodsTableData[i].price;
                row.addcartBuyType=erpOrderEditVm.goodsTableData[i].goodsType;
                row.pricingManner=erpOrderEditVm.goodsTableData[i].pricingManner;
                row.length=erpOrderEditVm.goodsTableData[i].length;
                row.width=erpOrderEditVm.goodsTableData[i].width;
                row.goodsId=erpOrderEditVm.goodsTableData[i].goodsId;
                row.copy=erpOrderEditVm.goodsTableData[i].copy;
                goodData.push(row);
            }
            //先拼接参数
            erpOrderEditVm.erpOrderItemDto.erpOrder=erpOrderEditVm.erpOrder;
            erpOrderEditVm.erpOrderItemDto.erpOrderItemList=goodData;
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/addStandardGoods",
                contentType: "application/json",
                data: JSON.stringify(erpOrderEditVm.erpOrderItemDto),
                success: function(r){
                    if(r.code === 0){
                        alert("添加商品成功!");
                        erpOrderEditVm.getInfo(erpOrderEditVm.erpOrder.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
            erpOrderEditVm.productDialogVisible=false;
        },
        //取消添加
        cancelAdd:function(){
            this.$confirm('此操作将取消添加产品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //将添加的商品数据清除:
                erpOrderEditVm.goodsTableData=[]
                erpOrderEditVm.goodsData=[]
                erpOrderEditVm.productDialogVisible=false;
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
         getCascaderObj(val, opt) {
            return val.map(function (value, index, array) {
                for (var itm in opt) {
                    if (opt[itm].value == value) {
                        var label = opt[itm].label;
                        opt = opt[itm].children;
                        return label;
                    }
                }
                return null;
             });
         },
        //商品明细列表查看单个商品详情
        itemDetail:function (row) {
		    var eoItemId=row.eoItemId;
            erpOrderEditVm.singleItemData=[];
            //查询
            var url="order/erporder/singleOrderItemDetail/"+eoItemId
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 0){
                        erpOrderEditVm.singleOrderItemDetail=r.singleOrderItemDetail;
                        erpOrderEditVm.singleItemData.push( erpOrderEditVm.singleOrderItemDetail);
                        erpOrderEditVm.orderItemDetailDialogVisible=true;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        //格式化分配情况展示
        formatterStatus:function (row, column, cellValue) {
            if (cellValue == '1') {
                return '待分配'
            } else if (cellValue == '2') {
                return '生产中'
            }else if(cellValue == '3'){
                return '已完成'
            }else if(cellValue == '4'){
                return '已发货'
            }
        },



        reback:function(){
		    //返回门店订单页面
            erpOrderEditVm.showEdit=false;
           vm.reload();
        },
        addPrintGoods:function (data) {
            var row=data[0];
            erpOrderEditVm.printGoods.productId=row.productId;
            erpOrderEditVm.printGoods.name=row.productName;
            erpOrderEditVm.printGoods.nums=row.quantity;
            erpOrderEditVm.printGoods.price=row.price;
            erpOrderEditVm.printGoods.amount=row.total;
            erpOrderEditVm.printGoods.addcartBuyType="fast";
            erpOrderEditVm.printGoods.goodsId=row.goodsId;
            erpOrderEditVm.printGoods.addon=row.craft;
            erpOrderEditVm.printGoods.calculationPriceProcess=row.calculationPriceProcess;
            erpOrderEditVm.printGoods.productInfoParamList=row.productInfoParamList;
            erpOrderEditVm.printGoods.attributeOption=row.attributeOption;
            erpOrderEditVm.printGoods.quoteSelfDefineParam=row.quoteSelfDefineParam;
            erpOrderEditVm.printGoods.cutModel=row.cutModel;
            var printGoods=[]
            printGoods.push(erpOrderEditVm.fastGoods);
            //先拼接参数
            erpOrderEditVm.erpOrderItemDto.erpOrder=erpOrderEditVm.erpOrder;
            erpOrderEditVm.erpOrderItemDto.erpOrderItemList=printGoods;
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/addPrintGoods",
                contentType: "application/json",
                data: JSON.stringify(erpOrderEditVm.erpOrderItemDto),
                success: function(r){
                    if(r.code === 0){
                        alert("添加商务印刷商品成功!");
                        erpOrderEditVm.printGoods.itemRemark="";
                        erpOrderEditVm.getInfo(erpOrderEditVm.erpOrder.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
            erpOrderEditVm.quotePriceDialogVisible=false;
            //清空选择和输入的数据
            erpOrderEditVm.printMoney=0;
            erpOrderEditVm.printIndex=2;
        },

        //添加快印商品
        addFastGoods:function(data){
            var row=data[0];
            erpOrderEditVm.fastGoods.productId=row.productId;
            erpOrderEditVm.fastGoods.name=row.productName;
            erpOrderEditVm.fastGoods.nums=row.quantity;
            erpOrderEditVm.fastGoods.price=row.price;
            erpOrderEditVm.fastGoods.amount=row.total;
            erpOrderEditVm.fastGoods.addcartBuyType=row.goodsType;
            erpOrderEditVm.fastGoods.goodsId=row.goodsId;
            erpOrderEditVm.fastGoods.addon=row.craft;
            erpOrderEditVm.fastGoods.calculationPriceProcess=row.calculationPriceProcess;
            erpOrderEditVm.fastGoods.productInfoParamList=row.productInfoParamList;
            erpOrderEditVm.fastGoods.attributeOption=row.attributeOption;
            erpOrderEditVm.fastGoods.quoteSelfDefineParam=row.quoteSelfDefineParam;
            erpOrderEditVm.fastGoods.cutModel=row.cutModel;
            erpOrderEditVm.fastGoods.copy=row.copy;
            var fastGoods=[]
            fastGoods.push(erpOrderEditVm.fastGoods);
            //先拼接参数
            erpOrderEditVm.erpOrderItemDto.erpOrder=erpOrderEditVm.erpOrder;
            erpOrderEditVm.erpOrderItemDto.erpOrderItemList=fastGoods;
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/addFastGoods",
                contentType: "application/json",
                data: JSON.stringify(erpOrderEditVm.erpOrderItemDto),
                success: function(r){
                    if(r.code === 0){
                        alert("添加商品成功!");
                        erpOrderEditVm.getInfo(erpOrderEditVm.erpOrder.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
            erpOrderEditVm.fastQuotePriceDialogVisible=false;
            //清空选择和输入的数据
            erpOrderEditVm.fastMoney=0;
        },

        //删除订单详情中的商品
        deleteGoods:function (index) {
            /* if(erpOrderEditVm.detailData.length==1){
                 alert("订单至少添加一个商品!");
                 return;
             }*/
            //erpOrderEditVm.detailData.splice(index,1);
            var eoItemId=erpOrderEditVm.detailData[index].eoItemId;
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/deleteGoods/"+eoItemId,
                contentType: "application/json",
                data: JSON.stringify(erpOrderEditVm.erpOrder),
                success: function(r){
                    if(r.code === 0){
                        alert("删除商品成功!");
                        erpOrderEditVm.getInfo(erpOrderEditVm.erpOrder.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        setMemberId:function(){

        },

        updateDiscount:function() {
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/updateDiscount",
                contentType: "application/json",
                data: JSON.stringify(erpOrderEditVm.erpOrder),
                success: function(r){
                    if(r.code === 0){
                        alert("调价成功!");
                        erpOrderEditVm.getInfo(erpOrderEditVm.erpOrder.orderNo);
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
    }
});
